/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.consultation;

import static controller.Connexion.CONF_DAO_INIT;
import static controller.administration.SaisieNotes.ATT_GESTION_COURS;
import dao.DAOInit;
import dao.interfaces.DAOCours;
import dao.interfaces.DAOEtudiant;
import dao.interfaces.DAOExamen;
import dao.interfaces.DAONote;
import gestionform.GestionCours;
import gestionform.GestionExamen;
import gestionform.GestionNote;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Cour;
import model.Enseignant;
import model.Etudiant;
import model.Examen;
import model.Note;

/**
 *
 * @author Paul
 */
public class DetailMatiere extends HttpServlet {

    public static final String VUE_DETAIL_MATIERE = "/WEB-INF/consultation/detailMatiere.jsp";
    private DAOCours daoCours;
    private DAOExamen daoExamen;
    private DAOEtudiant daoEtudiant;
    private DAONote daoNote;
    public static final String ATT_GESTION_EXAMEN = "gExamen";
    private static final String ATT_GESTION_NOTE = "gNote";

    @Override
    public void init() throws ServletException {
        /* Récupération d'une instance de notre DAO Personne */
        this.daoCours = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOCours();
        this.daoExamen = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOExamen();
        this.daoEtudiant = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAOEtudiant();
        this.daoNote = ((DAOInit) getServletContext().getAttribute(CONF_DAO_INIT)).getDAONote();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Etudiant e = (Etudiant) request.getSession().getAttribute("etudiant");
        if (e != null) {
            if (request.getSession().getAttribute("listeCours") == null) {
                HttpSession session = request.getSession(true);
                GestionCours gCours = new GestionCours(daoCours);
                ArrayList<Cour> listeCoursCoeff = gCours.getListeCoursCoeffEtudiant(e.getId());
                request.setAttribute(ATT_GESTION_COURS, gCours);
                session.setAttribute("listeCours", listeCoursCoeff);
                GestionExamen gExamen = new GestionExamen(daoExamen);
                ArrayList<Examen> listeExamen = new ArrayList<Examen>();
                ArrayList<Enseignant> listeEns = new ArrayList<Enseignant>();
                HashMap<Cour, Enseignant> listeCoursEns = new HashMap<Cour, Enseignant>();
                for (Cour cour : listeCoursCoeff) {
                    listeExamen.addAll(gExamen.getAllExamens(cour.getIdCour()));
                    listeEns.addAll(gCours.getProf(cour.getIdCour()));
                }
                for (Cour cour : listeCoursCoeff) {
                    for (Enseignant enseignant : listeEns) {
                        listeCoursEns.put(cour, enseignant);
                    }
                }

                session.setAttribute("listeCoursEns", listeCoursEns);
                request.setAttribute(ATT_GESTION_EXAMEN, gExamen);
                session.setAttribute("listeExamen", listeExamen);
                GestionNote gNote = new GestionNote(daoNote);
                HashMap<Examen, Note> listeExNote = new HashMap<Examen, Note>();
                for (Examen examen : listeExamen) {
                    listeExNote.put(examen, gNote.getNoteEtudiantExamen(e.getId(), examen.getId()));
                }
                request.setAttribute(ATT_GESTION_NOTE, gCours);
                session.setAttribute("listeExamNote", listeExNote);
            }
            request.getRequestDispatcher(VUE_DETAIL_MATIERE).forward(request, response);

        } else {
            request.getRequestDispatcher("view/connexion/connexion.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Etudiant e = (Etudiant) request.getSession().getAttribute("etudiant");
        if (e != null) {
            if (request.getSession().getAttribute("listeCours") == null) {
                HttpSession session = request.getSession(true);
                GestionCours gCours = new GestionCours(daoCours);
                ArrayList<Cour> listeCoursCoeff = gCours.getListeCoursCoeffEtudiant(e.getId());
                request.setAttribute(ATT_GESTION_COURS, gCours);
                ArrayList<Cour> listeCours = gCours.getListeCoursEtudiant(e.getId());
                session.setAttribute("listeCours", listeCoursCoeff);
                GestionExamen gExamen = new GestionExamen(daoExamen);
                ArrayList<Examen> listeExamen = new ArrayList<Examen>();
                for (Cour cour : listeCours) {
                    listeExamen.addAll(gExamen.getAllExamens(cour.getIdCour()));
                }
                session.setAttribute("listeExamen", listeExamen);
                GestionNote gNote = new GestionNote(daoNote);
                HashMap<Examen, Note> listeExNote = new HashMap<Examen, Note>();
                for (Examen examen : listeExamen) {
                    listeExNote.put(examen, gNote.getNoteEtudiantExamen(e.getId(), examen.getId()));
                }
                session.setAttribute("listeExamNote", listeExNote);
            }
            request.getRequestDispatcher(VUE_DETAIL_MATIERE).forward(request, response);

        } else {
            request.getRequestDispatcher("view/connexion/connexion.jsp").forward(request, response);
        }
    }
}